﻿using System.Web.Security;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin.Security;
using System;
using System.Linq;
using System.Web;
using System.Web.UI;
using RLAWEB.Models;

namespace RLAWEB.Account
{
    public partial class Login : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            RegisterHyperLink.NavigateUrl = "Register";
            OpenAuthLogin.ReturnUrl = Request.QueryString["ReturnUrl"];
            var returnUrl = HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
            if (!String.IsNullOrEmpty(returnUrl))
            {
                RegisterHyperLink.NavigateUrl += "?ReturnUrl=" + returnUrl;
            }
        }

        protected void LogIn(object sender, EventArgs e)
        {
#region comment 
            
            if (IsValid)
            {
                // Validate the user password
                //zzvar manager = new UserManager();
                var proxy = new RLAServiceReference.RealLifeServiceClient();
                var user = proxy.GetUser(UserName.Text, Password.Text);
                //ApplicationUser user = manager.Find(UserName.Text, Password.Text);
                if (user != null)
                {
                    string userdata = user.UserId.ToString();

                    var ticket = new FormsAuthenticationTicket(
                        1, 
                        user.ProfileName, 
                        DateTime.Now,
                        DateTime.Now.AddHours(1),
                        false,
                        userdata,
                        FormsAuthentication.FormsCookiePath);
                    
                    HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket));
                    authCookie.HttpOnly = true;
                    Response.Cookies.Add(authCookie);

                    Response.Redirect(FormsAuthentication.GetRedirectUrl(user.ProfileName, true));
                    
                }
                else
                {
                    FailureText.Text = "Invalid username or password.";
                    ErrorMessage.Visible = true;
                }
            }
             
#endregion
            
            /*if (IsValid)
            {
                // Validate the user password
                var manager = new UserManager();
                ApplicationUser user = manager.Find(UserName.Text, Password.Text);
                if (user != null)
                {
                    IdentityHelper.SignIn(manager, user, RememberMe.Checked);
                    IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
                }
                else
                {
                    FailureText.Text = "Invalid username or password.";
                    ErrorMessage.Visible = true;
                }
            }
            */
        }
    }
}